package com.estate.struts.actions;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.estate.business.ForumService;
import com.estate.utils.ForumUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.ForumCommentVo;
import com.estate.vo.impl.ForumVo;
import com.estate.vo.impl.UserInfoVo;

public class CommentAction extends EstateSupport {

	private String id;
	private String forumId;
	private UserInfoVo commentUser;
	private String content;
	private Date commentDate;
	private String replyId;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private ForumService forumService;
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			ForumCommentVo comment = this.getAddForumComment();
			if(comment != null){
				boolean result = this.getForumService().addComment(comment);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("update".equals(this.getMethod())){
			ForumCommentVo comment = this.getUpdateForumComment();
			if(comment == null){
				page = "fail";
			}else{
				boolean result = this.getForumService().updateComment(comment);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}else if("delete".equals(this.getMethod())){
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					ForumCommentVo comment = new ForumCommentVo();
					comment.setId(Integer.parseInt(this.getId()));
					boolean result = this.getForumService().deleteComment(comment);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
		}else{
			page="fail";
		}
		return page;
	}
	private ForumCommentVo getUpdateForumComment(){
		ForumCommentVo comment = new ForumCommentVo();
		
		comment.setContent(this.getContent());
		comment.setId(Integer.parseInt(this.getId()));
//		comment.setCommentDate(new Date());
		
		return comment;
	}
	private ForumCommentVo getAddForumComment(){
		ForumCommentVo comment = new ForumCommentVo();
		
		comment.setContent(this.getContent());
		comment.setCommentDate(new Date());
		comment.setCommentUser(this.getSessionUser());
		comment.setForumId(Integer.parseInt(this.getForumId()));
		
		if(this.getReplyId() == null && this.getReplyId().equals("")){
			this.setReplyId("0");
		}
		comment.setReplyId(Integer.parseInt(this.getReplyId()));
		
		return comment;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public UserInfoVo getCommentUser() {
		return commentUser;
	}
	public void setCommentUser(UserInfoVo commentUser) {
		this.commentUser = commentUser;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Date getCommentDate() {
		return commentDate;
	}
	public void setCommentDate(Date commentDate) {
		this.commentDate = commentDate;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public ForumService getForumService() {
		return forumService;
	}
	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}
	public String getForumId() {
		return forumId;
	}
	public void setForumId(String forumId) {
		this.forumId = forumId;
	}
	public String getReplyId() {
		return replyId;
	}
	public void setReplyId(String replyId) {
		this.replyId = replyId;
	}
}
